import axios from 'axios';
import type { QuestionType } from '../common/interfaces.ts'
// const API_BASE_URL = 'http://localhost:3000/question-types';
const API_BASE_URL = `${import.meta.env.VITE_API_BASE_URL}/resources/question-types`


// 获取学科列表
export const fetchQuestionTypes = async () => {
  try {
    const response = await axios.get(API_BASE_URL);
    // return response.data;
	return response;
  } catch (error) {
    // console.error('获取学科数据失败:', error);
    throw error;
  }
};

// 添加学科
export const addQuestionType = async (questionType: QuestionType) => {
  try {
    const response = await axios.post(`${API_BASE_URL}/`, questionType);
    // return response.data;
	return response;
  } catch (error) {
    // console.error('添加学科失败:', error);
    throw error;
  }
};

// 编辑学科
export const updateQuestionType = async (id: number, questionType: QuestionType) => {
  try {
    const response = await axios.patch(`${API_BASE_URL}/${id}/`, questionType);
    // return response.data;
	return response;
  } catch (error) {
    // console.error('编辑学科失败:', error);
    throw error;
  }
};

// 删除学科
export const deleteQuestionType = async (id: number) => {
  try {
    const response = await axios.delete(`${API_BASE_URL}/${id}/`);
	return response;
  } catch (error) {
    // console.error('删除学科失败:', error);
    throw error;
  }
};

export const searchQuestionType = async (searchData: object) => {
	try {
		const response = await axios.get(`${API_BASE_URL}?search_data=${JSON.stringify(searchData)}`);
		return response;
	} catch (error) {
	  // console.error('删除学科失败:', error);
	  throw error;
	}
}

export const searchPaginationQuestionType = async (page_size:number, current_page:number, searchData:object) => {
	/**
	 * 通过分页获取查询对象
	*/
	const response = await axios.get(`${API_BASE_URL}?page_size=${page_size}&current_page=${current_page}&search_data=${JSON.stringify(searchData)}`);
	return response;
}